{template header}
<style type="text/css">
    .bg{display:none;position:fixed;width:100%;height:100%;background:#000;z-index:2;top:0;left:0;opacity:0.7;}
</style>


<!--<form class="form-horizontal"  action="?c=adminErp&a=addErpProduct">-->
   <input type="hidden" id='pid' name="pid" value="{$pid}">
    <!--规格开始-->

    <h4 style="padding-left:10px;;">
        <a href="?c=adminErp&a=getErpProductList"><span>库存列表</span></a>
        >>
        <a href="?c=adminErp&a=getProductStandard&pid={$pid}"><span>补货</span></a> <!--<span class="btn btn-primary" id="addGg" >添加规格</span>-->
    </h4>
    <div class="control-group">
        <label class="control-label" >货品名：</label>
        <div class="controls" style="margin: -30px 10px 10px 60px;">
            <span style="line-height: 35px;">{if isset($list['title'])}{$list['title']}{/if}</span>
        </div>
    </div>
    
    {if !empty($list) && !empty($list['childGoods'])}
    <h4 style="padding-left:10px;;">
        <div style="margin: 10px 10px 10px 10px;">
            <span>入库货品</span>
        </div>
        <form action="?c=adminErp&a=replenishGoods" method="POST" id="form2">
        <table style="width: 100%" border="1" id="table">
            <thead>
                <tr>

                    {if count($info)>0}
                        <td>{loop $info $k $v}{$v['title']}&nbsp;&nbsp;&nbsp;&nbsp;{/loop}</td>
                    {else}
                    <td></td>
                    {/if}
                    <td>补货数量</td>
                    <td>货品成本</td>
                    <td>总价</td>
                </tr>
                {loop $list['childGoods'] $k2 $v2}
                    <tr>
                        <td>{$v2['attrPathName']}</td>
                        <td style="display: none"><input type="checkbox" name="gid[$k2]" class="gidInput" value="{$v2['gid']}" ></td>
                        <td><input type="text" name="stockIntoCnt[$k2]"  class="stockIntoInput"  placeholder="请输补货数量" /></td>
                        <td><input type="text" name="price[$k2]" class="priceInput"  placeholder="请输入货品成本" /></td>
                        <td><input type="text" readonly value="0" class="totalPriceInput" /></td>
                    </tr>
                {/loop}

            </thead>
        </table>
        </form>
    </h4>
    <span class="btn btn-primary" id="addRkOk" >确定入库</span>
{else}
    <a href="?c=adminErp&a=addErpProStandard&pid={$pid}"><span class="btn btn-primary">去添加规格商品+</span></a>
{/if}
<!--</form>-->

{template footer}
<div class="bg" id="bgx"></div>
<div id="dialog" style="position: absolute; z-index: 1003;background-color:white; display: none;" >
    <div  style="margin:50px 100px;">
        <div class="controls">
            <span id="gm" type="0">规格名：</span><input type="text" value="" id="remark" placeholder="规格名" /><br />
            <div style="text-align: center">
                <button class="btn btn-primary" id="ok" ids="" idflag = "">确定</button>
                <button class="btn btn-primary" id="close" >取消</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function(){
        $('.stockIntoInput').on('change',function(){
            var _this = $(this);
            if ($.trim(_this.val())) {
                _this.parent().siblings().find(".gidInput").prop('checked',true);
            } else {
                var _tx = _this.parent().siblings().find('.priceInput').val();
                if(!_tx) {
                    _this.parent().siblings().find('.gidInput').prop('checked',false);
                }
            }
        })

        $('.priceInput').on('change',function(){
            var _this = $(this);
            if ($.trim(_this.val())) {
                _this.parent().siblings().find('.gidInput').prop('checked',true);
            } else {
                var _tx = _this.parent().siblings().find('.stockIntoInput').val();
                if(!_tx) {
                    _this.parent().siblings().find('.gidInput').prop('checked',false);
                }
            }
        })
    });


    /*入库数量 单价联动*/
    $(document).on('focus keyup keydown','.stockIntoInput',function(){
        var _this=$(this),
                _priceInput = _this.parent().siblings().find('.priceInput'),
                _totalPriceInput = _this.parent().siblings().find('.totalPriceInput');
        var reg = new RegExp("^([1-9]+)([0-9]*)$"),reg2 = new RegExp(/^[0-9]+(\.[0-9]{1,2})?$/);
        if (!reg.test(_this.val())) {
            _totalPriceInput.val(0);
            return;
        }
        if (!reg2.test(_priceInput.val())) {
            _totalPriceInput.val(0);
            return;
        }
        var _total = _this.val()*_priceInput.val();
        _totalPriceInput.val(_total.toFixed(2))
    })

    $(document).on('focus keyup keydown','.priceInput',function(){
        var _this=$(this),
                _stockIntoInput = _this.parent().siblings().find('.stockIntoInput'),
                _totalPriceInput = _this.parent().siblings().find('.totalPriceInput');
        var reg = new RegExp("^([1-9]+)([0-9]*)$"),reg2 =  new RegExp(/^[0-9]+(\.[0-9]{1,2})?$/);
        console.log(reg2.test(_this.val()));
        if (!reg2.test(_this.val())) {
            _totalPriceInput.val(0);
            return;
        }
        if (!reg.test(_stockIntoInput.val())) {
            _totalPriceInput.val(0);
            return;
        }
        var _total = _this.val()*_stockIntoInput.val();
        _totalPriceInput.val(_total.toFixed(2))
    })


    $('#addRkOk').click(function () {
        $.ajax({
            type:'POST',
            url:$('#form2').attr('action'),
            data:$('#form2').serialize()+'&pid='+$('#pid').val(),
            dataType:'json',
            success:function(d){
                if(d.status=='ok'){
                    alert(d.error_msg);
                    window.location.href= d.url;
                }else{
                    alert(d.error_msg);
                }
            }
        })
    })



    $('#addGg').on('click',function(){
        $("#bgx").show();
        $('#ok').attr({ids:'',idflag:''});
        $('#remark').val('').attr('placeholder','规格名称');
        $('#gm').html('规格名称：').attr('type',0);
        letDivCenter('#dialog');
    })

    $("span[id^='addGgVal_']").on('click',function(){
        var _this =$(this),headId = _this.attr('headId');
        $("#bgx").show();
        $('#ok').attr({ids:'',idflag:headId});
        $('#remark').val('').attr('placeholder','多个规格值用|分隔');
        $('#gm').html('规格值：').attr('type',1);
        letDivCenter('#dialog');
    })

    $('#ok').on('click',function(){
        var _this = $(this),remark = $('#remark').val(),headId =_this.attr('idflag');
        if (!remark) {
            alert('请填写内容');return false;
        }
        if ($('#gm').attr('type') ==1) {
          var arrRemark = remark.split('|');
            if (arrRemark.length <=0) {
                alert('请填写内容');return false;
            }
//            $.each(arrRemark,function(i, n){
//                $('#aaaa_'+_this.attr('idflag')).append(
//                        '<span class="btn btn-primary"  style="margin: 10px 10px 10px 10px;" >'+n+'</span>'
//                );
//            })
            $.ajax({
                type:'POST',
                url:'?c=adminErp&a=addStandardVal',
                data:{headId:headId,strStandVal:remark},
                dataType:'json',
                success:function(d){
                    if(d.status=='ok'){
                        alert(d.error_msg);
                        window.location.reload();
                    }else{
                        alert(d.error_msg);
                    }
                }
            })


        } else {
            $.ajax({
                type:'POST',
                url:'?c=adminErp&a=addStandardHead',
                data:{pid:$('#pid').val(),name:$('#remark').val()},
                dataType:'json',
                success:function(d){
                    if(d.status=='ok'){
                        alert(d.error_msg);
                        window.location.reload();
                    }else{
                        alert(d.error_msg);
                    }
                }
            })

//            var time =new Date().getTime();
//            $('#gg_content').append(
//                    '<div class="control-group" id="gg_key_'+time+'">'
//                    +'<label class="control-label" >'+remark+'：</label><span class="btn btn-primary" id="addGgVal_'+time+'" fortime="'+time+'" >添加规格值</span>'
//                    +'<div class="controls" id="aacc_'+time+'">'
//                        //                +'<span class="btn btn-primary"  style="margin: 10px 10px 10px 10px;" >红色</span>'
//                    +'</div>'
//                    +'</div>'
//            );
        }

        $('#close').click();
    })
    $('#close').on('click',function(){
        $('#dialog').css('display','none');
        $("#bgx").fadeOut(300);
    })
    function letDivCenter(divName){
        var divObj = $(divName);
        var wWidth = $(document.body).width(),wHeight = $(window).height();
        var divWidth = divObj.width(),divHeight = divObj.height();
        divObj.css({'left':(wWidth-divWidth)/2+$(window).scrollLeft(),'top':(wHeight-divHeight)/2+$(window).scrollTop(),'display':'block'});
        $(window).bind('scroll resize',function(){
            $(divName).css({'left':(wWidth-divWidth)/2+$(window).scrollLeft(),'top':(wHeight-divHeight)/2+$(window).scrollTop()});
        });
    }

    function unique(arrRemark) {
            var n = {},r=[]; //n为hash表，r为临时数组
            for(var i = 0; i < arrRemark.length; i++) //遍历当前数组
            {
                if (!n[this[i]]) //如果hash表中没有当前项
                {
                    n[this[i]] = true; //存入hash表
                    r.push(this[i]); //把当前数组的当前项push到临时数组里面
                }
            }
            return r;
    }
</script>